﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VO.Web.Controllers;

namespace VO.Web.Account
{
    public partial class AdminUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Master.MessageText = string.Empty;
            }
        }

        protected bool GetIsAdmin(string key)
        {
            return MembershipController.GetIsAdmin(key);
        }

        protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string message = string.Empty;
            if (e.CommandName == "Unlock")
            {
                string userKey = e.CommandArgument.ToString();
                MembershipController.UnlockUser(userKey);
                gvUsers.DataBind();
                message = string.Format("User '{0}' has been unlocked", MembershipController.GetUserByKey(userKey));
            }
            else if (e.CommandName == "ChangeAdmin")
            {
                string userKey = e.CommandArgument.ToString();
                if (GetIsAdmin(userKey))
                {
                    MembershipController.RemoveUserFromAdmin(userKey);
                    message = string.Format("User '{0}' has been unpromoted", MembershipController.GetUserByKey(userKey) );
                }
                else
                {
                    MembershipController.AddUserToAdmin(userKey);
                    message = string.Format("User '{0}' has been promoted", MembershipController.GetUserByKey(userKey));
                }
                
            }
            Master.MessageText = message;
            gvUsers.DataBind();
        }
    }
}